package com.woniu.user.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.manage.client.NoticeClient;
import com.woniu.order.client.OrderQueryClient;
import com.woniu.order.client.param.SearchOrderParam;
import com.woniu.product.client.CouponQueryClient;
import com.woniu.product.client.FootPrintQueryClient;
import com.woniu.result.Result;
import com.woniu.user.client.UserInfoClient;
import com.woniu.user.client.param.RegisterParam;
import com.woniu.user.client.param.UpdateUserInfoParam;
import com.woniu.user.client.result.UserResultInfo;
import com.woniu.user.common.util.OssUtil;
import com.woniu.user.infra.model.UserAttachment;
import com.woniu.user.service.UserAccountService;
import com.woniu.user.service.UserAttachmentService;
import com.woniu.user.service.dto.UserInfoDto;
import com.woniu.user.service.param.RegisterServiceParam;
import com.woniu.user.service.param.UpdateUserInfoServiceParam;
import com.woniu.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.net.UnknownHostException;

@RestController
@Slf4j
public class UserInfoApiImpl implements UserInfoClient {

    @Resource
    private OrderQueryClient orderQueryClient;
    @Resource
    private UserAccountService userAccountService;

    @Resource
    private UserAttachmentService userAttachmentService;

    @Resource
    private NoticeClient noticeClient;

    @Resource
    private CouponQueryClient couponQueryClient;

    @Resource
    private FootPrintQueryClient footPrintQueryClient;


    /**
     * 登录
     *
     * @param userPhone
     * @param pwd
     * @return
     */
    @Override
    public Result login(String userPhone, String pwd) throws UnknownHostException {
        log.info("检查用户名:{}", userPhone);
        String token = userAccountService.login(userPhone, pwd);
        if (token != null) return Result.getInstance(String.class).setData(token).setMsg("登录成功");
        return Result.getInstance(String.class).setMsg("登录失败");
    }

    /**
     * 注册
     *
     * @param registerParam
     * @return
     */
    @Override
    public Result register(RegisterParam registerParam) {
        log.info("检查注册信息:{}", registerParam);
        RegisterServiceParam registerServiceParam = BeanUtil.copyProperties(registerParam, RegisterServiceParam.class);
        boolean register = userAccountService.register(registerServiceParam);
        if (register) return Result.getInstance().setMsg("注册成功");
        return Result.getInstance().setMsg("注册失败");
    }

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    @Override
    public Result sendCode(String phone) {
        log.info("检查手机号码:{}", phone);
        boolean b = userAccountService.sendCode(phone);
        if (b) return Result.getInstance().setMsg("发送成功");
        return Result.getInstance().setMsg("发送失败");
    }

    /**
     * 完善信息
     *
     * @param updateUserInfoParam
     * @return
     */
    @Override
    public Result updateUserInfo(UpdateUserInfoParam updateUserInfoParam) {
        log.info("查看个人信息:{}", updateUserInfoParam);
        UpdateUserInfoServiceParam updateUserInfoServiceParam = BeanUtil.copyProperties(updateUserInfoParam, UpdateUserInfoServiceParam.class);
        UserInfoDto userInfoDto = userAccountService.updateInfo(updateUserInfoServiceParam);
        if (userInfoDto != null) return Result.getInstance(UserInfoDto.class).setMsg("修改成功").setData(userInfoDto);
        return Result.getInstance().setMsg("修改失败");
    }

    /**
     * 查看用户信息
     *
     * @return
     */
    @Override
    public Result<UserResultInfo> searchUserInfo(Integer userId) {
        log.info("用户id:{}", userId);
        UserInfoDto userInfoDto = userAccountService.getUserInfo(userId);
        UserResultInfo userResultInfo = BeanUtil.copyProperties(userInfoDto, UserResultInfo.class);
        return Result.getInstance(UserResultInfo.class).setMsg("查询成功").setData(userResultInfo);
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public Result uploadFile(MultipartFile file) throws Exception {
        log.info("查看路径：{}", file);
        String filename = file.getOriginalFilename();
        String type = filename.substring(filename.lastIndexOf("."));
        String name = System.currentTimeMillis() + type;
        String path = OssUtil.uploadFile("woniuxy84", name, file.getInputStream());
        UserAttachment ua = new UserAttachment();
        ua.setOldfileName(filename);
        ua.setUserId(1);
        ua.setFileName(name);
        ua.setFilePath(path);
        ua.setFileSuffix(type);
        ua.setSize(file.getSize());
        ua.setCreateDate(DateUtil.parseDateLong());
        userAttachmentService.save(ua);
        return Result.getInstance(String.class).setMsg("上传成功").setData(path);
    }

    /**
     * 阅读公告
     * @param noticeId
     * @return
     */
    @Override
    public Result userReadNotice(Integer userId,Integer noticeId) {
        //TODO userId
        return noticeClient.userReadNotice(userId, noticeId);
    }

    /**
     * 查看订单状态
     * @param orderState
     * @return
     */
    @Override
    public Result orderState(Integer userId,String orderState) {
        SearchOrderParam orderParam = new SearchOrderParam();
        orderParam.setOrderState(orderState);
        orderParam.setUserId(userId);
        return orderQueryClient.searchOrder(orderParam);
    }

    @Override
    public Result receiveRecord(Integer userId,String state) {
        return couponQueryClient.receiveRecord(userId, state);
    }

    @Override
    public Result getUserFoot(Integer userId) {
        return footPrintQueryClient.getUserFootPrint(userId);
    }
}
